package com.yoda.youdianda.controller;

import com.yoda.youdianda.common.R;
import com.yoda.youdianda.domain.YddAdvimg;
import com.yoda.youdianda.service.YddAdvimgService;
import com.yoda.youdianda.vo.PageResult;
import com.yoda.youdianda.vo.YddAdvimgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {
    @Autowired
    private YddAdvimgService yddAdvimgService;

    @GetMapping("/list")
    public R list(String advimgdesc, String advimgpos, Integer page, Integer limit) {
        PageResult<YddAdvimgVo> yddAdvimgVoPageResult = yddAdvimgService.getImgPage(advimgdesc, advimgpos, page, limit);
        return R.success(yddAdvimgVoPageResult);
    }

    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvimg yddAdvimg) {
        yddAdvimgService.updateById(yddAdvimg);
        return R.success("修改成功");
    }

    @GetMapping("/del")
    public R<String> del(int id) {
        yddAdvimgService.removeById(id);
        return R.success("删除成功");
    }

    @PostMapping("/add")
    public R<String> add(HttpSession session, @RequestBody YddAdvimg yddAdvimg) {
        String res="http(s)?://\\w{3,}(\\.\\w+)+(\\.\\w{2,3})+";
        if(yddAdvimg.getAdvimgsrc().matches(res)){}

        yddAdvimg.setAdvimgsrc((String) session.getAttribute("imgName"));
        yddAdvimgService.save(yddAdvimg);
        return R.success("添加成功");
    }

    @GetMapping("/rowInfo")
    public R<YddAdvimg> getById(int id) {
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        return R.success(yddAdvimg);

    }
}
